<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>语音识别与翻译</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }
        .container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            background: white;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        input[type="file"] {
            margin: 20px 0;
            display: block;
        }
        button {
            padding: 10px 15px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
        }
        button:hover {
            background-color: #218838;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background: #f9f9f9;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>语音识别与翻译</h1>
    <input type="file" id="audioFile" accept="audio/wav">
    <button id="uploadBtn">上传并翻译</button>
    <div class="result" id="resultContainer" style="display: none;">
        <h3>识别结果:</h3>
        <p id="recognizedText"></p>
        <h3>翻译结果:</h3>
        <p id="translatedText"></p>
    </div>
</div>

<script>
    document.getElementById('uploadBtn').addEventListener('click', function() {
        const fileInput = document.getElementById('audioFile');
        const file = fileInput.files[0];
        if (!file) {
            alert("请上传音频文件！");
            return;
        }

        const formData = new FormData();
        formData.append('audio', file);

        fetch('/process_audio', {
            method: 'POST',
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            document.getElementById('recognizedText').innerText = data.recognizedText || '未识别到文本';
            document.getElementById('translatedText').innerText = data.translatedText || '翻译失败';
            document.getElementById('resultContainer').style.display = 'block';
        })
        .catch(error => {
            console.error('错误:', error);
            alert('上传或处理音频时出错');
        });
    });
</script>

</body>
</html>